import { asyncRoutes, constantRoutes } from '@/router'

const state = {
  routes: []
}
const mutations = {
  setRoutes(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
const actions = {
  // 创建过滤路由的函数
  // 需要用户的menus和所有的动态路由配置
  // 如果menus中有路由名称,则该用户可以访问该路由
  filterRoutes(store, menus) { // 传入store模块,菜单
    const routes = asyncRoutes.filter(item => menus.includes(item.name))
    console.log(routes)
    // 调用mutations
    store.commit('setRoutes', routes)
    // 返回routes
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
